package com.quan.xczs.rpc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quan.base.common.base.service.support.BaseService;
import com.quan.base.common.exception.BizException;
import com.quan.xczs.rpc.api.XczsConfigService;
import com.quan.xczs.rpc.api.XczsCouponService;
import com.quan.xczs.rpc.dao.mapper.XczsCouponMapper;
import com.quan.xczs.rpc.model.XczsCoupon;
import com.quan.xczs.rpc.model.XczsUserCoupon;

@Service
public class XczsCouponServiceImpl extends BaseService<XczsCoupon, Integer> implements XczsCouponService {
	
	@Autowired
	private XczsCouponMapper mapper ;
	
	@Autowired
	private XczsConfigService configService ;
	
	@Override
	public List<XczsUserCoupon> getCouponCombobox() {
		return this.mapper.getCouponCombobox();
	}
	
	@Override
	public int delete(Integer id) throws BizException {
		//清空全局配置中的优惠券关联
		this.configService.setCouponEmpty(id);
		return super.delete(id);
	}
	
	@Override
	public int deleteByIds(String ids) throws BizException {
		String[] split = ids.split(",");
		for (String id : split) {
			//清空全局配置中的优惠券关联
			this.configService.setCouponEmpty(Integer.parseInt(id));
		}
		return super.deleteByIds(ids);
	}

	@Override
	public void uploadActivateCount(Integer id) {
		XczsCoupon record = super.selectById(id);
		record.setActivateCount(record.getActivateCount()+1);
		super.update(record) ;
	}

	@Override
	public void uploadConsumeCount(Integer id) {
		XczsCoupon record = super.selectById(id);
		record.setConsumeCount(record.getConsumeCount()+1);
		super.update(record) ;
	}

	@Override
	public XczsCoupon getCouponMoneyByOpenId(String openid) {
		return this.mapper.getCouponMoneyByOpenId(openid);
	}

}
